package signup.domain;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;


public class UsersService {
	EntityManagerFactory emf =
        Persistence.createEntityManagerFactory("pu");
	@SuppressWarnings("unchecked")
	public boolean authenticateUser(String emailAddress, String password){
		List<Users> userList=emf.createEntityManager().createQuery("from Users u where u.emailAddress=? and u.password=?" ).setParameter(1, emailAddress).setParameter(2, password).getResultList();
		
		if(userList.size()>0)
			return true;
		else
			return false;
	}
	@SuppressWarnings("unchecked")
	public boolean activateUser(String emailAddress, int key){
		EntityManager emg=emf.createEntityManager();
		List<Users> userList=emg.createQuery("from Users u where u.emailAddress=? and u.randomNumber=?" ).setParameter(1, emailAddress).setParameter(2, key).getResultList();
		if(userList.size()<0)
			return false;
		else
		{
			for(Users u: userList)
			{
				EntityTransaction tx=emg.getTransaction();
				tx.begin();
				u.setIsActive(true);
				u=emg.merge(u);
				emg.persist(u);
				tx.commit();
				
			}
			
			return true;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Users> getAllUsers()
	{
		
		List<Users> userList=emf.createEntityManager().createQuery("from Users u").getResultList();
		return userList;
	}
	
	@SuppressWarnings("unchecked")
	public List<Users> findUsersByEmailAddress(String EmailAddress)
	{
		
		List<Users> userList=emf.createEntityManager().createQuery("from Users u where u.emailAddress =?").setParameter(1,EmailAddress).getResultList();
		return userList;
	}
	
	@SuppressWarnings("unchecked")
	public Users updateUser(Users u)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            u=jpa.merge(u);
            jpa.persist(u);
            tx.commit();
            
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
            
        }
		return u;
	}
	
	public static void main(String[] args) {
		
		UsersService u=new UsersService();
		Users user1=new Users("sam.walter@hotmail.com","sam","6513532569","mypassword");
		EntityManager emg=Persistence.createEntityManagerFactory("pu").createEntityManager();
		EntityTransaction tx=emg.getTransaction();
		tx.begin();
		emg.persist(user1);
		tx.commit();
		
		System.out.print(u.authenticateUser("sam.walter@hotmail.com", "mypassword"));
		
		List<Users> a=u.findUsersByEmailAddress("sam.walter@hotmail.com");
		
		for(int i=0;i<a.size();i++)
		{
			System.out.println(u.activateUser(a.get(i).getEmailAddress(), a.get(i).getRandomNumber()));
			
		}
		
	}	
	
	
}
